package com.wangsd.web.shop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.basicbean.excel.ExcelData;
import com.wangsd.common.basicbean.excel.POIUtil;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.web.shop.entity.Trade;
import com.wangsd.web.shop.service.TradeService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "交易管理")
@RestController
@RequestMapping("/shop/trade")
@Slf4j
public class TradeController extends MyController {
    @Autowired
    private TradeService service;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             Trade queryObj) {
        if (StringUtils.isNotBlank(queryObj.getEndTime())) {
            queryObj.setEndTime(queryObj.getEndTime() + " 23:59:59");
        }
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Trade> list = service.selectPage(queryObj, loginUser.getParentCode());
        PageInfo pageInfo = getPageInfo(page, list);

        //获取当前条件的订单信息统计
//        Map statistics = service.selectStatistics(queryObj, loginUser.getParentCode());
        Map result = new HashMap();
        result.put("pageInfo", pageInfo);
//        result.put("statistics", statistics);
        return Result.success(result);
    }

    @ApiOperation(value = "导出交易明细")
    @PostMapping("/exportExcel")
    public void exportExcel(Trade queryObj) {
        if (StringUtils.isNotBlank(queryObj.getEndTime())) {
            queryObj.setEndTime(queryObj.getEndTime() + " 23:59:59");
        }
        SysUser loginUser = getUserInfo();
        List<Trade> list = service.selectPage(queryObj, loginUser.getParentCode());
        ExcelData excelData = new ExcelData();
        List<String> titles = new ArrayList<>();
        ArrayList<List<Object>> rowList = new ArrayList<>();//行
        titles.add("商品名称");
        titles.add("用户昵称");
        titles.add("客户订单号");
        titles.add("交易订单号");
        titles.add("订单金额");
        titles.add("订单状态");
        titles.add("领取状态");
        titles.add("订单创建时间");
        excelData.setTitles(titles);
        for (Trade trade : list) {
            List<Object> strList = new ArrayList<>();
            strList.add(trade.getProductName());
            strList.add(trade.getNickName());
            strList.add(trade.getOutTradeNo());
            strList.add(trade.getTransactionId());
            strList.add(trade.getTransactionId() == null ? trade.getPrice() : trade.getPrice()*0.01);
            switch (trade.getStatus()) {
                case 0:
                    strList.add("未支付");
                    break;
                case 1:
                    strList.add("已支付");
                    break;
                case 2:
                    strList.add("已退款");
                    break;
            }
            switch (trade.getSendStatus()) {
                case 1:
                    strList.add("未领取");
                    break;
                case 2:
                    strList.add("已领取");
                    break;
            }
            strList.add(DateUtils.formatDatetime(trade.getCreatedTime()));
            rowList.add(strList);
        }
        excelData.setRows(rowList);
        try {
            POIUtil.exportExcel(response, "文件", excelData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
